package com.lixin.web.messageCodec;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageCodec;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Description: 复杂消息转换器没搞懂 下次在说
 * Copyright:   Copyright (c)2023
 * Company:     sci
 *
 * @author: 张李鑫
 * @version: 1.0
 * Create at:   2023-07-26 22:47:32
 * <p>
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2023-07-26     张李鑫                     1.0         1.0 Version
 */
@Component
@ChannelHandler.Sharable
public class MessageCodec extends MessageToMessageCodec<String, Object> {

    private ObjectMapper objectMapper = new ObjectMapper();

    @Override
    protected void encode(ChannelHandlerContext ctx, Object msg, List<Object> out) throws Exception {
        System.out.println("server encode");
        out.add(objectMapper.writeValueAsString(msg));
    }

    @Override
    protected void decode(ChannelHandlerContext ctx, String msg, List<Object> out) throws Exception {
        System.out.println("server decode");
        out.add(objectMapper.readValue(msg, Object.class));
    }
}
